/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SlidePanelForm.java
 *
 * Created on 28/11/2009, 01:24:23
 */

package com.power.visual;

import com.power.utils.PowerImage;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author f6036477
 */
public class SlidePanelForm 
    extends javax.swing.JPanel
{

  private boolean isDownArrow;

  private PowerImage upArrow, downArrow;

  private JFrame floating;

  private JPanel panel;

  private int maxHeight;


    /** Creates new form SlidePanelForm */
    public SlidePanelForm()
    {
        upArrow = new PowerImage(this.getClass()
            .getResource("circle-arrow-up-[25].png"));
        downArrow = new PowerImage(this.getClass()
            .getResource("circle-arrow-down-[25].png"));

        floating = new JFrame();
        floating.setUndecorated(true);
        floating.setLayout(null);
        floating.setAlwaysOnTop(true);
        panel = new JPanel();
        panel.setLayout(null);
        panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        panel.setLocation(0, 0);
        floating.add(panel);

        initComponents();
        isDownArrow = true;

        arrowButton.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent ae)
          {
            if(isDownArrow) {
              slideOut();
              arrowButton.setImage(upArrow);
            } else {
              slideIn();
              arrowButton.setImage(downArrow);
            }
            isDownArrow = !isDownArrow;
          }
        });

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    titleLabel = new javax.swing.JLabel();
    separator = new javax.swing.JSeparator();
    arrowButton = new com.power.visual.OvalImageButton();

    titleLabel.setText("Slide Panel");

    arrowButton.setImage(downArrow);

    javax.swing.GroupLayout arrowButtonLayout = new javax.swing.GroupLayout(arrowButton);
    arrowButton.setLayout(arrowButtonLayout);
    arrowButtonLayout.setHorizontalGroup(
      arrowButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 26, Short.MAX_VALUE)
    );
    arrowButtonLayout.setVerticalGroup(
      arrowButtonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 26, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(arrowButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(arrowButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(layout.createSequentialGroup()
            .addComponent(titleLabel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
  }// </editor-fold>//GEN-END:initComponents

    
    public void setContent(Component c) 
    {
      if(c == null) return;
      maxHeight = c.getHeight()+ 10;
      c.setLocation(5, 5);
      panel.add(c);
    }

    public void setTitle(String title)
    {
      titleLabel.setText(title);
    }

    public String getTitle()
    {
      return titleLabel.getText();
    }

    public void setFlotingHeight(int height)
    {
      maxHeight = height;
    }

    @Override
    public void setSize(int w, int h)
    {
      if(w < 1 || h < 1) return;

      super.setSize(w, h);
      separator.setSize(w -63, separator.getHeight());
      titleLabel.setSize(separator.getWidth() -5,
          titleLabel.getHeight());
      arrowButton.setLocation(
          separator.getLocation().x + separator.getWidth() + 6,
          arrowButton.getLocation().y);
    }

    private void slideIn()
    {
      if(!this.isVisible()) return;

      for(int i = floating.getHeight(); i >= 2; i -= 5)
      {
        floating.setSize(floating.getWidth(), i);
        try {
          Thread.sleep(2);
        } catch (InterruptedException ex) {}
      }
      floating.setVisible(false);
    }

    private void slideOut()
    {
      if(!this.isVisible()) return;

      floating.setLocation(separator.getLocationOnScreen().x,
          separator.getLocationOnScreen().y+ 3);
      floating.setSize(separator.getWidth(), maxHeight);
      panel.setSize(separator.getWidth(), maxHeight);
      floating.setVisible(true);
    }


  // Variables declaration - do not modify//GEN-BEGIN:variables
  private com.power.visual.OvalImageButton arrowButton;
  private javax.swing.JSeparator separator;
  private javax.swing.JLabel titleLabel;
  // End of variables declaration//GEN-END:variables

  public static void main(String[] args)
  {
    JFrame f = new JFrame("SlidePanelForm");
    SlidePanelForm sp = new SlidePanelForm();
    sp.setFlotingHeight(200);
    f.add(sp);
    f.setLocation(300, 300);
    f.setSize(310, 230);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }


}
